using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Lzfy_His_Service.Models
{
    /// <summary>
    /// 坐席账号信息模型
    /// </summary>
    [Table("lzfy_agent_account")]
    public class AgentAccount
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        [Key]
        [Column("id")]
        public int Id { get; set; }

        /// <summary>
        /// 数据录入时间
        /// </summary>
        [Column("input_date")]
        public DateTime? InputDate { get; set; }

        /// <summary>
        /// API地址
        /// </summary>
        [Column("apiurl")]
        [StringLength(50)]
        public string? ApiUrl { get; set; }

        /// <summary>
        /// 来源分机号
        /// </summary>
        [Column("FromExten")]
        [StringLength(50)]
        public string? FromExten { get; set; }

        /// <summary>
        /// 分机号
        /// </summary>
        [Column("Exten")]
        [StringLength(50)]
        public string? Exten { get; set; }

        /// <summary>
        /// 账户ID
        /// </summary>
        [Column("ACCOUNTID")]
        [StringLength(50)]
        public string? AccountId { get; set; }

        /// <summary>
        /// API密钥
        /// </summary>
        [Column("APISecret")]
        [StringLength(50)]
        public string? ApiSecret { get; set; }

        /// <summary>
        /// 删除标记 (0:可用, 1:已删除)
        /// </summary>
        [Column("Deleted")]
        public short? Deleted { get; set; }

        /// <summary>
        /// 登录时间
        /// </summary>
        [Column("LoginDate")]
        public DateTime? LoginDate { get; set; }

        /// <summary>
        /// 登出时间
        /// </summary>
        [Column("LogoutDate")]
        public DateTime? LogoutDate { get; set; }

        /// <summary>
        /// 最后操作时间
        /// </summary>
        [Column("LastActionDate")]
        public DateTime? LastActionDate { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        [Column("username")]
        [StringLength(50)]
        public string? Username { get; set; }

        /// <summary>
        /// 用户全名
        /// </summary>
        [Column("fullname")]
        [StringLength(50)]
        public string? FullName { get; set; }

        /// <summary>
        /// 行锁定标记
        /// </summary>
        [Column("rowLock")]
        [StringLength(50)]
        public string? RowLock { get; set; }

        /// <summary>
        /// 坐席当前电话
        /// </summary>
        [Column("userPhone")]
        [StringLength(20)]
        public string? UserPhone { get; set; }
    }

    /// <summary>
    /// 坐席账号申请请求模型
    /// </summary>
    public class AgentAccountApplyRequest
    {
        /// <summary>
        /// 用户名
        /// </summary>
        [Required(ErrorMessage = "用户名不能为空")]
        public string Username { get; set; } = string.Empty;

        /// <summary>
        /// 用户全名
        /// </summary>
        [Required(ErrorMessage = "用户全名不能为空")]
        public string FullName { get; set; } = string.Empty;

        /// <summary>
        /// 用户电话号码
        /// </summary>
        [StringLength(20, ErrorMessage = "电话号码长度不能超过20个字符")]
        public string? UserPhone { get; set; }
    }

    /// <summary>
    /// 坐席账号确认请求模型
    /// </summary>
    public class AgentAccountConfirmRequest
    {
        /// <summary>
        /// 账号ID
        /// </summary>
        [Required(ErrorMessage = "账号ID不能为空")]
        public int AccountId { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        [Required(ErrorMessage = "用户名不能为空")]
        public string Username { get; set; } = string.Empty;

        /// <summary>
        /// 是否确认 (true:确认, false:取消确认)
        /// </summary>
        public bool IsConfirm { get; set; }

        /// <summary>
        /// 坐席当前电话
        /// </summary>
        [StringLength(20, ErrorMessage = "电话号码长度不能超过20个字符")]
        public string? UserPhone { get; set; }
    }

    /// <summary>
    /// 坐席账号操作记录请求模型
    /// </summary>
    public class AgentAccountActionRequest
    {
        /// <summary>
        /// 账号ID
        /// </summary>
        [Required(ErrorMessage = "账号ID不能为空")]
        public int AccountId { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        [Required(ErrorMessage = "用户名不能为空")]
        public string Username { get; set; } = string.Empty;
    }

    /// <summary>
    /// 坐席账号响应模型
    /// </summary>
    public class AgentAccountResponse
    {
        /// <summary>
        /// 账号ID
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// API地址
        /// </summary>
        public string? ApiUrl { get; set; }

        /// <summary>
        /// 来源分机号
        /// </summary>
        public string? FromExten { get; set; }

        /// <summary>
        /// 分机号
        /// </summary>
        public string? Exten { get; set; }

        /// <summary>
        /// 账户ID
        /// </summary>
        public string? AccountId { get; set; }

        /// <summary>
        /// API密钥
        /// </summary>
        public string? ApiSecret { get; set; }

        /// <summary>
        /// 登录时间
        /// </summary>
        public DateTime? LoginDate { get; set; }

        /// <summary>
        /// 登出时间
        /// </summary>
        public DateTime? LogoutDate { get; set; }

        /// <summary>
        /// 最后操作时间
        /// </summary>
        public DateTime? LastActionDate { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string? Username { get; set; }

        /// <summary>
        /// 用户全名
        /// </summary>
        public string? FullName { get; set; }

        /// <summary>
        /// 坐席当前电话
        /// </summary>
        public string? UserPhone { get; set; }
    }

    /// <summary>
    /// 坐席使用状态统计响应模型
    /// </summary>
    public class AgentAccountStatusResponse
    {
        /// <summary>
        /// 坐席总数
        /// </summary>
        public int TotalAccounts { get; set; }

        /// <summary>
        /// 使用中的坐席数量
        /// </summary>
        public int InUseAccounts { get; set; }

        /// <summary>
        /// 可申请的坐席数量
        /// </summary>
        public int AvailableAccounts { get; set; }

        /// <summary>
        /// 申请中的坐席数量（已锁定但未确认）
        /// </summary>
        public int PendingAccounts { get; set; }

        /// <summary>
        /// 非活跃的坐席数量（超过非活跃时间阈值）
        /// </summary>
        public int InactiveAccounts { get; set; }

        /// <summary>
        /// 过期的坐席数量（LogoutDate已过期）
        /// </summary>
        public int ExpiredAccounts { get; set; }

        /// <summary>
        /// 锁定超时的坐席数量（rowLockDate超过1分钟）
        /// </summary>
        public int LockTimeoutAccounts { get; set; }

        /// <summary>
        /// 统计时间
        /// </summary>
        public DateTime StatisticsTime { get; set; }

        /// <summary>
        /// 配置的非活跃时间阈值（分钟）
        /// </summary>
        public int InactiveMinutes { get; set; }

        /// <summary>
        /// 配置的使用时长（分钟）
        /// </summary>
        public int UsageDurationMinutes { get; set; }
    }

    /// <summary>
    /// 用户坐席状态查询请求模型
    /// </summary>
    public class UserAgentStatusRequest
    {
        /// <summary>
        /// 用户名
        /// </summary>
        [Required(ErrorMessage = "用户名不能为空")]
        [StringLength(50, MinimumLength = 1, ErrorMessage = "用户名长度必须在1-50个字符之间")]
        [RegularExpression(@"^[a-zA-Z0-9_\-\.@]+$", ErrorMessage = "用户名只能包含字母、数字、下划线、连字符、点和@符号")]
        public string Username { get; set; } = string.Empty;
    }

    /// <summary>
    /// 用户坐席状态查询响应模型
    /// </summary>
    public class UserAgentStatusResponse
    {
        /// <summary>
        /// 是否有坐席账号
        /// </summary>
        public bool HasAccount { get; set; }

        /// <summary>
        /// 账号状态 (none:无账号, pending:申请中, active:使用中, expired:已过期, inactive:非活跃过期, lock_timeout:锁定超时)
        /// </summary>
        public string AccountStatus { get; set; } = "none";

        /// <summary>
        /// 坐席账号信息
        /// </summary>
        public AgentAccountResponse? Account { get; set; }

        /// <summary>
        /// 剩余锁定时间（分钟）
        /// </summary>
        public double? RemainingLockMinutes { get; set; }

        /// <summary>
        /// 剩余使用时间（分钟）
        /// </summary>
        public double? RemainingUsageMinutes { get; set; }
    }
}